package com.wyyc.common.login.controller;

import com.alibaba.fastjson.JSON;
import com.wyyc.common.login.service.LoginService;
import com.wyyc.entity.ResJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Hwei~ on 2017/6/6.
 */
@Controller
@RequestMapping("request/user/login")
public class LoginController {



    @Resource
    private LoginService loginService;

    /**
     * 获得登陆验证码
     * @param phone 手机号
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "sendCode",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String sendCode(String phone){
        if (phone!=null&&!phone.equals("")){
            ResJson resJson = loginService.sendCode(phone);
            return JSON.toJSONString(resJson);
        }
        return "";
    }

    @RequestMapping(value = "subLogin",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String subLogin(String phone,String code){
        if (phone!=null&&!phone.equals("")){
            if (code!=null&&!code.equals("")){
                ResJson resJson = loginService.subLogin(phone, code);
                return JSON.toJSONString(resJson);
            }
        }
        return "";
    }

    @RequestMapping(value = "autoLogin",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String autoLogin(Integer uid){
        if (uid!=null&&uid!=0){
            ResJson resJson = loginService.autoLogin(uid);
            return JSON.toJSONString(resJson);
        }
        return "";
    }
}
